package com.ddstation.statement.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import com.ddstation.crud.entity.DtInvsManageInstitutionEntity;
import com.ddstation.home_page.model.HpCommonParModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ddstation.common.util.DateUtil;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.statement.model.IllegalInstitutionModel;
import com.ddstation.statement.model.IllegalTypeModel;
import com.ddstation.statement.model.SearchParModel;
import com.ddstation.statement.service.CATALOG_ENUM;
import com.ddstation.statement.service.StatisticsStatementService;

@Controller
public class StatisticsStatementController {

    /**
     * 统计报表初始页面 跳转
     *
     * @param
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "invs_statistics_statement.do")
    public String statisticsStatementInitEvent(
            Model model,
            HttpSession session) {

        // 从session获取基础数据
        SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
        if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
            return "forward:login?timeout=true"; // session 过期
        }

        return "pc/statement/statistics_statement";
    }

    /**
     * 统计报表数据查询
     * @param startDt
     * @param endDt
     * @param session
     * @return
     */
    @RequestMapping(value = "invs_statistics_statement.do/load", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> loadStatisticsStatementListEvent(
            @RequestParam String startDt,
            @RequestParam String endDt,
            HttpSession session) {

        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("errcode", -1);
        ret.put("errMsg", "系统出错，请稍后再试！");

        //日期check
        if (StringUtils.isBlank(startDt) || StringUtils.isBlank(endDt)) {
            ret.put("errMsg", "输入参数错误，请刷新页面后再试！");
            return ret;
        }

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            format.parse(startDt);
            format.parse(endDt);

            SessionLoginData loginData = (SessionLoginData) session.getAttribute(SessionLoginData.KEY);
            DtInvsManageInstitutionEntity institution = loginData.getInstitution();
            HpCommonParModel _p = new HpCommonParModel();
            _p.setDtFrom(startDt);
            _p.setDtTo(endDt);
            _p.setProvince(Integer.valueOf(institution.getProvince()));
            _p.setCity(Integer.valueOf(institution.getCity()));
            _p.setCounty(Integer.valueOf(institution.getCounty()));

            HashMap statisticsData = statisticsStatementService.getStatisticsData(_p);
            ret.put("statisticsData", statisticsData);

        } catch (ParseException e) {
            ret.put("errMsg", "输日期格式必须为：yyyy-MM-dd");
            return ret;

        } catch (Exception e) {
            ret.put("errMsg", e.getMessage());
            return ret;
        }

        ret.put("errcode", 0);
        ret.put("errMsg", "");
        return ret;
    }

    @Inject
    private StatisticsStatementService statisticsStatementService;

}
